package com.itheima.controller;

import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*
    TODO: springmvc基于AOP封住的异常统一处理器
 */
@RestControllerAdvice
public class ProjectExceptionAdvice2 {

    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException ex){
        //发送对应消息传递给用户，提醒规范操作
//        ex.printStackTrace();
        String message = ex.getMessage();
        Integer code = ex.getCode();
        return new Result(code,message);
    }

    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
//        发送固定消息传递给用户，安抚用户
//        发送特定消息给运维人员，提醒维护
//                记录日志
//        ex.printStackTrace();
        String message = ex.getMessage();
        Integer code = ex.getCode();
//        System.out.println("系统异常了,具体原因如下: ");
//        ex.printStackTrace();
        return new Result(code,message);
    }

    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex){
        ex.printStackTrace();
//        System.out.println("出现未知异常,具体原因如下: ");
//        ex.printStackTrace();
        return new Result(Code.SYSTEM_UNKNOW_ERR,"未知异常");
    }

}
